λ¬Έμ
Programmers - νμΌλͺ
μ λ ¬
π Logic
fileList.sort(((o1, o2) -> {
// headκ° λμΌνλ€λ©΄, number κΈ°μ€ μ€λ¦μ°¨μ μ λ ¬
if (o1.head.compareTo(o2.head) == 0) return o1.number - o2.number;
// head κΈ°μ€ μ¬μ μ μ λ ¬
return o1.head.compareTo(o2.head);
}));
head κ° λμΌν κ²½μ°μ λμΌνμ§ μμ κ²½μ°μ λν μ λ ¬ 쑰건 λͺ
μ
π CODE
μ½λ 보기/μ κΈ°π«
import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
class File {
// λ³ννμ§ μμ μλ³Έ νμΌλͺ
String file;
String head;
int number;
public File(String file) {
this.file = file;
file = file.toLowerCase();
// μ«μκ° μ°μμ΄κ±°λ, μ«μκ° μλ λ¬Έμκ° μ°μμΈ κ²½μ° μ°ΎκΈ°
Pattern p = Pattern.compile("\\d+|\\D+");
Matcher matcher = p.matcher(file);
matcher.find();
this.head = matcher.group(0);
matcher.find();
this.number = Integer.parseInt(matcher.group(0));
}
}
class Solution {
public String[] solution(String[] files) {
ArrayList<File> fileList = new ArrayList<>();
for (String file : files)
fileList.add(new File(file));
fileList.sort(((o1, o2) -> {
// headκ° λμΌνλ€λ©΄, number κΈ°μ€ μ€λ¦μ°¨μ μ λ ¬
if (o1.head.compareTo(o2.head) == 0) return o1.number - o2.number;
// head κΈ°μ€ μ¬μ μ μ λ ¬
return o1.head.compareTo(o2.head);
}));
String[] answer = new String[files.length];
for (int i = 0; i < fileList.size(); i++)
answer[i] = fileList.get(i).file;
return answer;
}
}
βοΈ Review
head μ number μ λν μ λ ¬ μ‘°κ±΄λ§ μΈμ΄λ€λ©΄ ν΄κ²°λλ λ¬Έμ !